#!/usr/bin/env python

import sys
import logging
from logging import *
from singleton import Singleton

#to use logger, you should first call init_log(), then call get_logger, and we should use singleton model to use this , refer to singleton.py

_default_log_config = [['debug.log', '', 'debug'],
						['error.log', '', 'error']]

class StaffLogger(object):
	def __init__(self , **args):
		if not args.has_key('log_name'):
			raise Exception
		self._log_name = args['log_name']
	
	def LOG(self):
		return logging.getLogger(self._log_name)

def init_log(log_config = None):

	log_format =  logging.Formatter('%(name)-12s %(asctime)s %(levelname)-8s %(message)s',	'%a, %d %b %Y %H:%M:%S')

	if not log_config:
		log_config = _default_log_config
	
	for log in log_config:
		logger = logging.getLogger(log[0])	
		if log[1]:
			handler = logging.FileHandler(log[1], 'a')
		else:
			handler = logging.StreamHandler(sys.stderr)
		handler.setFormatter(log_format)		
		logger.addHandler(handler)
		logger.setLevel(log[2].upper())


if __name__ == '__main__':
	init_log(_default_log_config)
	test_log = Singleton.getInst(StaffLogger, log_name = 'debug.log')	
	test_log.LOG().debug('test error function')
